//
//  GDBuslLineViewCell.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/1/13.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//

#import "GDBuslLineViewCell.h"
#import "GDBusStationLinesModel.h"
#import "GDNowBusModel.h"
#import "NSString+Extension.h"
@interface GDBuslLineViewCell ()

@property(nonatomic,strong) NSArray * aModel;

@property(nonatomic,weak) UIImageView * nowBusIcon;
@end

#define showColNumber 2
@implementation GDBuslLineViewCell


-(void)layoutSubviews{
   
    [super layoutSubviews];
    
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UIView * nowBusView = [[UIView alloc] init];
        [self addSubview:nowBusView];
        _nowBusView = nowBusView;
        
    
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        imageView.image = [UIImage imageNamed:@"bus_location_icon"];
        [nowBusView addSubview:imageView];
        _nowBusIcon = imageView;
        
    }
    return self;
}

-(void)setLineModel:(GDBusStationLinesModel *)lineModel{
    
    [self currentBusStation:lineModel.Name];
    self.backgroundColor = [UIColor clearColor];
    // 左侧图
    self.imageView.image = [UIImage imageNamed:@"bus_dic_icon"];
    // 站点名称
    self.textLabel.text = lineModel.Name;

    self.textLabel.font = [UIFont systemFontOfSize:14];
    CGSize statsSize = [self.textLabel.text sizeWithMaxSize:CGSizeMake(100, MAXFLOAT) fontSize:13];
//    NSLog(@"Width ： %lf",statsSize.width);
    self.nowBusView.frame = CGRectMake(statsSize.width + 80, 5,self.frame.size.width * 0.5, 40);
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
}

-(void) currentBusStation:(NSString *) stationName{
    BOOL isExists = false;
    NSMutableArray * existsBusArray = [NSMutableArray array];
    for (int index = 0; index < self.nowBusArray.count; index++) {
        GDNowBusModel * nowModel = self.nowBusArray[index];
        if ([stationName isEqualToString:nowModel.CurrentStation]) {
             // 显示当前站有
            isExists = true;
            [existsBusArray addObject:nowModel];

        }
    }
    if (!isExists) {
        // 不存在公交
        self.nowBusView.hidden = YES;
    }else{
         self.nowBusView.hidden = NO;
        for ( int index = 0; index <existsBusArray.count ; index ++) {
            int col = index % showColNumber;
            int row = index / showColNumber;
            GDNowBusModel * nowModel = existsBusArray[index];
            UILabel * busNumber = [[UILabel alloc] init];
            busNumber.text = nowModel.BusNumber;
            [busNumber sizeToFit];
            busNumber.font = [UIFont boldSystemFontOfSize:10];
            busNumber.center = CGPointMake(CGRectGetMaxX(self.nowBusIcon.frame) + col * 50 + 30, GDMargin + row * 30);
            [self.nowBusView addSubview:busNumber];
        }
    }
}




@end
